﻿using System;
using System.Collections.Generic;
using TimelineService.Utils;
using Windows.UI.Xaml.Automation.Text;

namespace TimelineService.Inis {
    public sealed class AppIni {
        private readonly HashSet<string> PROVIDER = new HashSet<string>() {
            A3gIni.GetId(),
            AbyssIni.GetId(),
            AdwestIni.GetId(),
            BackieeIni.GetId(),
            BingIni.GetId(),
            ColorhubIni.GetId(),
            EsoIni.GetId(),
            FavoriteIni.GetId(),
            GluttonIni.GetId(),
            Himawari8Ini.GetId(),
            HuamaoIni.GetId(),
            IuiIni.GetId(),
            LocalIni.GetId(),
            LspIni.GetId(),
            NasaIni.GetId(),
            NetbianIni.GetId(),
            ObzhiIni.GetId(),
            OneIni.GetId(),
            PexelsIni.GetId(),
            QingbzIni.GetId(),
            SimpleIni.GetId(),
            SkitterIni.GetId(),
            SnakeIni.GetId(),
            TimelineIni.GetId(),
            ToopicIni.GetId(),
            VenusIni.GetId(),
            YmyouliIni.GetId(),
            WallhavenIni.GetId(),
            WallhereIni.GetId(),
            WallpaperupIni.GetId(),
            ZzzmhIni.GetId()
        };

        public string User { set; get; } = "";

        public string UserEncoded() {
            return TextUtil.Encode(User);
        }

        public string Pwd { set; get; } = "";

        public string PwdEncrypted() {
            return TextUtil.Md5(Pwd);
        }

        public string Client { set; get; } = "";

        private string provider = BingIni.GetId();
        public string Provider {
            set => provider = PROVIDER.Contains(value) ? value : BingIni.GetId();
            get => provider;
        }

        private string folder = "";
        public string Folder {
            //set => folder = string.Concat((value ?? "").Split(Path.GetInvalidFileNameChars()));
            set => folder = value ?? "";
            get => folder;
        }

        public int Cache { set; get; } = 600;

        public string DesktopProvider { set; get; }

        public string LockProvider { set; get; }

        public A3gIni A3g { set; get; } = new A3gIni();
        public AbyssIni Abyss { set; get; } = new AbyssIni();
        public AdwestIni Adwest { set; get; } = new AdwestIni();
        public BackieeIni Backiee { set; get; } = new BackieeIni();
        public BingIni Bing { set; get; } = new BingIni();
        public ColorhubIni Colorhub { set; get; } = new ColorhubIni();
        public EsoIni Eso { set; get; } = new EsoIni();
        public FavoriteIni Favorite { set; get; } = new FavoriteIni();
        public GluttonIni Glutton { set; get; } = new GluttonIni();
        public Himawari8Ini Himawari8 { set; get; } = new Himawari8Ini();
        public HuamaoIni Huamao { set; get; } = new HuamaoIni();
        public IuiIni Iui { set; get; } = new IuiIni();
        public LocalIni Local { set; get; } = new LocalIni();
        public LspIni Lsp { set; get; } = new LspIni();
        public NasaIni Nasa { set; get; } = new NasaIni();
        public NetbianIni Netbian { set; get; } = new NetbianIni();
        public ObzhiIni Obzhi { set; get; } = new ObzhiIni();
        public OneIni One { set; get; } = new OneIni();
        public PexelsIni Pexels { set; get; } = new PexelsIni();
        public QingbzIni Qingbz { set; get; } = new QingbzIni();
        public SimpleIni Simple { set; get; } = new SimpleIni();
        public SkitterIni Skitter { set; get; } = new SkitterIni();
        public SnakeIni Snake { set; get; } = new SnakeIni();
        public TimelineIni Timeline { set; get; } = new TimelineIni();
        public ToopicIni Toopic { set; get; } = new ToopicIni();
        public VenusIni Venus { set; get; } = new VenusIni();
        public WallhavenIni Wallhaven { set; get; } = new WallhavenIni();
        public WallhereIni Wallhere { set; get; } = new WallhereIni();
        public WallpaperupIni Wallpaperup { set; get; } = new WallpaperupIni();
        public YmyouliIni Ymyouli { set; get; } = new YmyouliIni();
        public ZzzmhIni Zzzmh { set; get; } = new ZzzmhIni();

        public float GetDesktopPeriod(string provider) {
            Dictionary<string, float> dict = new Dictionary<string, float>() {
                { A3gIni.GetId(), A3g.DesktopPeriod },
                { AbyssIni.GetId(), Abyss.DesktopPeriod },
                { AdwestIni.GetId(), Adwest.DesktopPeriod },
                { BackieeIni.GetId(), Backiee.DesktopPeriod },
                { BingIni.GetId(), Bing.DesktopPeriod },
                { ColorhubIni.GetId(), Colorhub.DesktopPeriod },
                { EsoIni.GetId(), Eso.DesktopPeriod },
                { FavoriteIni.GetId(), Favorite.DesktopPeriod },
                { GluttonIni.GetId(), Glutton.DesktopPeriod },
                { Himawari8Ini.GetId(), Himawari8.DesktopPeriod },
                { HuamaoIni.GetId(), Huamao.DesktopPeriod },
                { IuiIni.GetId(), Iui.DesktopPeriod },
                { LocalIni.GetId(), Local.DesktopPeriod },
                { LspIni.GetId(), Lsp.DesktopPeriod },
                { NasaIni.GetId(), Nasa.DesktopPeriod },
                { NetbianIni.GetId(), Netbian.DesktopPeriod },
                { ObzhiIni.GetId(), Obzhi.DesktopPeriod },
                { OneIni.GetId(), One.DesktopPeriod },
                { PexelsIni.GetId(), Pexels.DesktopPeriod },
                { QingbzIni.GetId(), Qingbz.DesktopPeriod },
                { SimpleIni.GetId(), Simple.DesktopPeriod },
                { SkitterIni.GetId(), Skitter.DesktopPeriod },
                { SnakeIni.GetId(), Snake.DesktopPeriod },
                { TimelineIni.GetId(), Timeline.DesktopPeriod },
                { ToopicIni.GetId(), Toopic.DesktopPeriod },
                { VenusIni.GetId(), Venus.DesktopPeriod },
                { WallhavenIni.GetId(), Wallhaven.DesktopPeriod },
                { WallhereIni.GetId(), Wallhere.DesktopPeriod },
                { WallpaperupIni.GetId(), Wallpaperup.DesktopPeriod },
                { YmyouliIni.GetId(), Ymyouli.DesktopPeriod },
                { ZzzmhIni.GetId(), Zzzmh.DesktopPeriod }
            };
            return dict.GetValueOrDefault(provider, Bing.DesktopPeriod);
        }

        public float GetLockPeriod(string provider) {
            Dictionary<string, float> dict = new Dictionary<string, float>() {
                { A3gIni.GetId(), A3g.LockPeriod },
                { AbyssIni.GetId(), Abyss.LockPeriod },
                { AdwestIni.GetId(), Adwest.LockPeriod },
                { BackieeIni.GetId(), Backiee.LockPeriod },
                { BingIni.GetId(), Bing.LockPeriod },
                { ColorhubIni.GetId(), Colorhub.LockPeriod },
                { EsoIni.GetId(), Eso.LockPeriod },
                { FavoriteIni.GetId(), Favorite.LockPeriod },
                { GluttonIni.GetId(), Glutton.LockPeriod },
                { Himawari8Ini.GetId(), Himawari8.LockPeriod },
                { HuamaoIni.GetId(), Huamao.LockPeriod },
                { IuiIni.GetId(), Iui.LockPeriod },
                { LocalIni.GetId(), Local.LockPeriod },
                { LspIni.GetId(), Lsp.LockPeriod },
                { NasaIni.GetId(), Nasa.LockPeriod },
                { NetbianIni.GetId(), Netbian.LockPeriod },
                { ObzhiIni.GetId(), Obzhi.LockPeriod },
                { OneIni.GetId(), One.LockPeriod },
                { PexelsIni.GetId(), Pexels.LockPeriod },
                { QingbzIni.GetId(), Qingbz.LockPeriod },
                { SimpleIni.GetId(), Simple.LockPeriod },
                { SkitterIni.GetId(), Skitter.LockPeriod },
                { SnakeIni.GetId(), Snake.LockPeriod },
                { TimelineIni.GetId(), Timeline.LockPeriod },
                { ToopicIni.GetId(), Toopic.LockPeriod },
                { VenusIni.GetId(), Venus.LockPeriod },
                { WallhavenIni.GetId(), Wallhaven.LockPeriod },
                { WallhereIni.GetId(), Wallhere.LockPeriod },
                { WallpaperupIni.GetId(), Wallpaperup.LockPeriod },
                { YmyouliIni.GetId(), Ymyouli.LockPeriod },
                { ZzzmhIni.GetId(), Zzzmh.LockPeriod }
            };
            return dict.GetValueOrDefault(provider, Bing.LockPeriod);
        }

        override public string ToString() {
            Dictionary<string, string> dict = new Dictionary<string, string>() {
                { A3gIni.GetId(), A3g.ToString() },
                { AbyssIni.GetId(), Abyss.ToString() },
                { AdwestIni.GetId(), Adwest.ToString() },
                { BackieeIni.GetId(), Backiee.ToString() },
                { BingIni.GetId(), Bing.ToString() },
                { ColorhubIni.GetId(), Colorhub.ToString() },
                { EsoIni.GetId(), Eso.ToString() },
                { FavoriteIni.GetId(), Favorite.ToString() },
                { GluttonIni.GetId(), Glutton.ToString() },
                { Himawari8Ini.GetId(), Himawari8.ToString() },
                { HuamaoIni.GetId(), Huamao.ToString() },
                { IuiIni.GetId(), Iui.ToString() },
                { LocalIni.GetId(), Local.ToString() },
                { LspIni.GetId(), Lsp.ToString() },
                { NasaIni.GetId(), Nasa.ToString() },
                { NetbianIni.GetId(), Netbian.ToString() },
                { ObzhiIni.GetId(), Obzhi.ToString() },
                { OneIni.GetId(), One.ToString() },
                { PexelsIni.GetId(), Pexels.ToString() },
                { QingbzIni.GetId(), Qingbz.ToString() },
                { SimpleIni.GetId(), Simple.ToString() },
                { SkitterIni.GetId(), Skitter.ToString() },
                { SnakeIni.GetId(), Snake.ToString() },
                { TimelineIni.GetId(), Timeline.ToString() },
                { ToopicIni.GetId(), Toopic.ToString() },
                { VenusIni.GetId(), Venus.ToString() },
                { WallhavenIni.GetId(), Wallhaven.ToString() },
                { WallhereIni.GetId(), Wallhere.ToString() },
                { WallpaperupIni.GetId(), Wallpaperup.ToString() },
                { YmyouliIni.GetId(), Ymyouli.ToString() },
                { ZzzmhIni.GetId(), Zzzmh.ToString() }
            };
            string paras = dict.GetValueOrDefault(provider, Bing.ToString());
            return $"/{Provider}?desktopprovider={DesktopProvider}&lockprovider={LockProvider}"
                + $"&user={UserEncoded()}"
                + (paras.Length > 0 ? "&" : "") + paras;
        }
    }
}
